module(..., package.seeall)

-----------------------------------------------------------------------------
-- Splits a string on a delimiter. 
-- Adapted from http://lua-users.org/wiki/SplitJoin.
-- 
-- @param text           the text to be split.
-- @param delimiter      the delimiter.
-- @return               unpacked values.
-----------------------------------------------------------------------------
function split(text, delimiter)
   local list = {}
   local pos = 1
   if string.find("", delimiter, 1) then 
      error("delimiter matches empty string!")
   end
   while 1 do
      local first, last = string.find(text, delimiter, pos)
      if first then -- found?
	 table.insert(list, string.sub(text, pos, first-1))
	 pos = last+1
      else
	 table.insert(list, string.sub(text, pos))
	 break
      end
   end
   return unpack(list)
end

-----------------------------------------------------------------------------
-- Turns a string into something that can be used as a page name, converting
-- - ? + = % ' " / \ and spaces to '_'.  Note this isn't enought to ensure
-- legal win2k names, since we _are_ allowing [ ] + < > : ; *, but we'll rely
-- on versium to escape those later.
-- 
-- @param text           the string to be dirified.
-- @return               the dirified string.
-----------------------------------------------------------------------------
function dirify(text)
   local pattern = [[[%?%+%=%%%s%'%"%\]+]]
   return (text or ""):gsub(pattern, "_")
end

-----------------------------------------------------------------------------
-- Escapes a text for using in a text area.
-- 			
-- @param text           the text to be escaped.
-- @return               the escaped text.
-----------------------------------------------------------------------------
function escape(text) 
   text = tostring(text or "")
   text = text:gsub("&", "&amp;"):gsub(">","&gt;"):gsub("<","&lt;")
   return text:gsub("\"", "&quot;")
end


function format_time(timestamp, format, tzoffset, tzname)
   if tzoffset == "local" then  -- calculate local time zone (for the server)
      local now = os.time()
      local local_t = os.date("*t", now)
      local utc_t = os.date("!*t", now)
      local delta = (local_t.hour - utc_t.hour)*60 + (local_t.min - utc_t.min)
      local h, m = math.modf( delta / 60)
      tzoffset = string.format("%+.4d", 100 * h + 60 * m)
   end
   tzoffset = tzoffset or "GMT"
   format = format:gsub("%%z", tzname or tzoffset)
   if tzoffset == "GMT" then 
      tzoffset = "+0000"
   end
   tzoffset = tzoffset:gsub(":", "")

   local sign = 1
   if tzoffset:sub(1,1) == "-" then
      sign = -1
      tzoffset = tzoffset:sub(2)
   elseif tzoffset:sub(1,1) == "+" then
      tzoffset = tzoffset:sub(2)       
   end
   tzoffset = sign * (tonumber(tzoffset:sub(1,2))*60 + tonumber(tzoffset:sub(3,4)))*60
   return os.date(format, os.time{ year=timestamp:sub(1,4), month=timestamp:sub(6,7),
                                   day=timestamp:sub(9,10), hour=timestamp:sub(12,13),
                                   min=timestamp:sub(15,16), sec=timestamp:sub(18)} + tzoffset)
end